//
//  FXUserAccount.swift
//  微博(Swift)
//
//  Created by V on 16/1/1.
//  Copyright © 2016年 V. All rights reserved.
//

import UIKit

/**
 "access_token": "ACCESS_TOKEN",
 "expires_in": 1234,
 "remind_in":"798114",
 "uid":"12341234"
*/
class FXUserAccount: NSObject,NSCoding {
    // 访问令牌
    var access_token: String?
    // 过期时间
    var expires_in: NSTimeInterval = 0 {
        
        didSet{
            // 取到当前时间加上 expires_in 这么多秒之后的一新时间，就是过期时间
            expiresDate = NSDate().dateByAddingTimeInterval(expires_in)
        }
    }
    var expiresDate: NSDate?
    var remind_in: String?
    // 用户的 ID
    var uid: String?
    // 头像
    var avatar_large: String?
    // 昵称
    var screen_name: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    
    // 归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token,forKey: "access_token")
        aCoder.encodeObject(expiresDate,forKey: "expiresDate")
        aCoder.encodeObject(remind_in,forKey: "remind_in")
        aCoder.encodeObject(uid,forKey: "uid")
        aCoder.encodeObject(avatar_large,forKey: "avatar_large")
        aCoder.encodeObject(screen_name,forKey: "screen_name")
    }
    
    // 解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        remind_in = aDecoder.decodeObjectForKey("remind_in") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    }
    
    
    // 重写属性
    override var description: String{
        let keys = ["uid", "expires_in", "access_token", "remind_in","avatar_large","screen_name"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
}
